// pages/user_center/travelagency/friendcarcomp/addfriendcomp/addfriendcomp.js
var Tool = require("../../../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    complist: [],
    searchkey: '',
    page: 1,
    searchkey: '',
    inputShowed: false,
    inputVal: "",
  },

  inputTyping: function (e) {
    this.setData({
      searchkey: e.detail.value
    })
  },

  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },

  searchgo: function () {
    this.data.page = 1
    this.search()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getfriendcomp()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    });
    this.getfriendcomp()
  },

  getfriendcomp: function () {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    console.log(that.data.page)
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CooPeratePartners.ashx',
      data: {
        ID: useinfo.ID,
        CType: 1,
        Info: 1,
        Conditions: that.data.searchkey,
        PageIndex: that.data.page,
        PageSize: 20,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            complist: that.data.complist.concat(data.data),
          });
        }
        wx.hideToast()
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  //搜索点击事件
  search: function () {
    var that = this
    that.setData({
      complist: [],
    });
    that.getfriendcomp()
  },

  //输入操作
  inputchange: function (e) {
    var that = this
    console.log(e)

    that.setData({
      searchkey: e.detail.value,
    });
  },

  addcomp: function (e) {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定将 ' + e.currentTarget.dataset.name + ' 设为合作车公司',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'https://www.lanshuicar.com/Server/AgencyServer/AgencyAddCoopPartners.ashx',
            data: {
              ID: useinfo.ID,
              PID: e.currentTarget.dataset.partnerid,
              Type: 3,
              OpType: 0,
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              console.log(res)
              if (res.data.code == 0) {
                wx.navigateBack()
                wx.showToast({
                  title: '添加成功',
                  icon: 'succes',
                  duration: 1000,
                })
              } else if (res.data.code == -2) {
                that.showError('合作车公司已经存在')
              }
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  }
})